import java.util.*;
import java.util.stream.Collectors;

public class Leetcode636 {
    public static void main(String[] args) {
        //[20,14,35,7,6,9,10,14]
        System.out.println(Arrays.toString(exclusiveTime(8, List.of(
                "0:start:0", "1:start:5", "2:start:6", "3:start:9", "4:start:11", "5:start:12", "6:start:14",
                "7:start:15", "1:start:24", "1:end:29", "7:end:34", "6:end:37", "5:end:39", "4:end:40",
                "3:end:45", "0:start:49", "0:end:54", "5:start:55", "5:end:59", "4:start:63", "4:end:66", "2:start:69", "2:end:70", "2:start:74", "6:start:78",
                "0:start:79", "0:end:80", "6:end:85", "1:start:89", "1:end:93", "2:end:96", "2:end:100", "1:end:102", "2:start:105", "2:end:109", "0:end:114"
        ))));
    }

    public static int[] exclusiveTime(int n, List<String> logs) {
        int[] res = new int[n];

        Stack<int[]> stack = new Stack<>();

        for (String log : logs) {
            String[] logArray = log.split(":");

            if (logArray[1].equals("start")||stack.isEmpty()) {
                stack.add(new int[]{Integer.parseInt(logArray[0]), Integer.parseInt(logArray[2])});
            } else {
                int[] currentStart = stack.pop();
                int time = Integer.parseInt(logArray[2]) - currentStart[1] + 1;
                res[currentStart[0]] += time;

                if (!stack.isEmpty()) {
                    res[stack.peek()[0]] -= time;
                }
            }

        }


        return res;
    }

//    for (Log log : logList) {
//
//        if (log.status.equals(Status.start)||stack.isEmpty()) {
//            stack.add(log);
//        } else {
//            Log currentStart = stack.pop();
//            int time = log.time - currentStart.time + 1;
//            res[currentStart.name] += time;
//
//            if (!stack.isEmpty()) {
//                Log next = stack.peek();
//                res[next.name] -= time;
//            }
//        }
//
//    }

}
